﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Microsoft.Data.ConnectionUI;
using Ionic.Zip;
namespace RuleCode.ObjectTree
{
    public partial class Configuration : Form
    {
        XmlDocument aots;
         XmlNode aotsnode;
        public Configuration()
        {
        
            InitializeComponent();
            init();
        }
        private void init()
        {
            
            aots = new XmlDocument();
            aots.Load(Util.GetProfilePath());
            aotsnode = aots.SelectSingleNode("//aots");
            XmlNodeList nodes = aots.SelectNodes("//aot");
            comboBox1.Items.Clear();
            foreach (XmlNode node in nodes)
            {
              //  Console.WriteLine(node.InnerText);
                comboBox1.Items.Add(node.Attributes.GetNamedItem("Name").Value);
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();

            XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "configuration", "");
            xmldoc.AppendChild(xmlnode);

            XmlElement x_aot = xmldoc.CreateElement("", "configuration", "");
            xmldoc.AppendChild(x_aot);

            XmlElement x_datadictionary = xmldoc.CreateElement("", "aots", "");


            x_aot.AppendChild(x_datadictionary);

            string configName = "Default";
            XmlElement aot = xmldoc.CreateElement("aot");
            aot.SetAttribute("Name", configName);
            aot.SetAttribute("ConnectionString", "ConnectionString");
            x_datadictionary.AppendChild(aot);


            xmldoc.Save(Util.GetProfilePath());
            string dir = System.IO.Path.Combine(Util.GetUserDataPath() + GlobalStaticDirectoryPath.AOTSProfilePath, configName);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            Microsoft.Data.ConnectionUI.DataConnectionDialog _dialog = new
Microsoft.Data.ConnectionUI.DataConnectionDialog();
            _dialog.DataSources.Add(DataSource.SqlDataSource);
            _dialog.SelectedDataProvider = DataProvider.SqlDataProvider;      
            if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(_dialog)
 == DialogResult.OK)
            {
                textBox2.Text = _dialog.ConnectionString;
            }
            _dialog.Dispose();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox1.SelectedItem.ToString()))
            {

                XmlNode select = aots.SelectSingleNode("//aot[@Name='" + comboBox1.SelectedItem + "']");
                if (select != null)
                {
                    textBox1.Text = select.Attributes.GetNamedItem("Name").Value;
                    textBox2.Text = select.Attributes.GetNamedItem("ConnectionString").Value;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (isEditing)
            {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    XmlNode select = aots.SelectSingleNode("//aot[@Name='" + oldConfigName + "']");
                  
                    aotsnode.RemoveChild(select);
                     XmlElement aot = aots.CreateElement("aot");
                    aot.SetAttribute("Name", textBox1.Text);
                    aot.SetAttribute("ConnectionString", textBox2.Text);
                    aotsnode.AppendChild(aot);
                    AfterNewAndEdit();
                   
                }
 
            }
            else
            {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                  
                    XmlElement aot = aots.CreateElement("aot");
                    aot.SetAttribute("Name", textBox1.Text);
                    aot.SetAttribute("ConnectionString", textBox2.Text);
                    aotsnode.AppendChild(aot);

                    AfterNewAndEdit();
                }
            }

        }
        private void AfterNewAndEdit()
        {
            aots.Save(Util.GetProfilePath());
            init();
              string dir = System.IO.Path.Combine(Util.GetUserDataPath() + GlobalStaticDirectoryPath.AOTSProfilePath, textBox1.Text);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            //unpack the template zip
         
         //ZipFile必须手动指定Encoding为Default，否则压缩包中的中文文件名成乱码。
            using (ZipFile zip1 = ZipFile.Read(Util.ApplicationBasePath + "\\template.zip",Encoding.Default))
            {
                
                // here, we extract every entry, but we could extract conditionally
                // based on entry name, size, date, checkbox status, etc.  
                foreach (ZipEntry e in zip1)
                {
                    e.Extract(dir, ExtractExistingFileAction.OverwriteSilently);
                }
            }
}
        string oldConfigName;
        string newConfigName;
        bool isEditing;
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
            textBox2.ReadOnly = false;
            oldConfigName = comboBox1.SelectedItem.ToString() ;
            isEditing = true;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            XmlDocument configFile = new XmlDocument();
            configFile.Load(Util.GetConfigFilePath());
            XmlNode currentprofilesname = configFile.SelectSingleNode("//currentprofilename");
            currentprofilesname.FirstChild.Value = comboBox1.SelectedItem.ToString();
            configFile.Save(Util.GetConfigFilePath());
        }

     
       
    }
}
